package site.assad.dao;

import org.springframework.stereotype.Repository;
import site.assad.domain.User;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2018/2/11 21:45
 * Description:
 */
@Repository
public class UserDao {

    //这里为了方便演示，不连接数据库操作，直接用一个列表模拟数据库
    private List<User> db = Arrays.asList(
            new User(0,"assad","2333","icon-001"),
            new User(1,"vancy","44455","icon-002")
    );

    public User getUserById(int userId){
        for(User user : db)
            if(user.getId() == userId)
                return db.get(userId);
        return null;
    }

    public void deleteUserById(int userId){
        for(User user : db)
            if(user.getId() == userId)
                db.remove(user);
    }

    public boolean addUser(User user){
        for(User u : db)
            if(u.getId() == user.getId())
                return false;
        db.add(user);
        return true;
    }

    public boolean update(User user){
        for(int i = 0; i < db.size(); i++){
            if(db.get(i).getId() == user.getId()){
                db.set(i,user);
                return true;
            }
        }
        return false;
    }

}
